package com.Policing.Repairs;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.Policing.Since_case.Since_case;
import com.demo.WaveSwipeRefreshLayout;
import com.Policing.Adapter.Addrepairs_Adapter;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.bean.AddRepairs_bean;
import com.Policing.bean.Property_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/18.
 */

public class Repairs_property extends Activity {
    private ListView lv_poperty;
    private TextView title_text;
    private ImageView fh;
    private List<AddRepairs_bean> data = new ArrayList<>();
    private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;
    private String repairsId;
    private String url="http://"+ Ip_Set.IP+"/api/repairsQueryById";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repairs_property);
        inview();
        Since_case.set().setIsRemarks("故障描述");
        load();
    }

    private void inview() {
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("报修列表");
        Intent intent = getIntent();
        final String state = intent.getStringExtra("state");
        repairsId = intent.getStringExtra("repairsId");
        lv_poperty = (ListView) findViewById(R.id.repairs_list);
        lv_poperty.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if (state.equals("已审核")){
                    Intent intent1 = new Intent(Repairs_property.this,Repairs_Scheme.class);
                    intent1.putExtra("name", data.get(position).getAsset_name());
                    intent1.putExtra("number", data.get(position).getCode_number());
                    intent1.putExtra("describe",data.get(position).getRemark());
                    intent1.putExtra("assetDetailId",data.get(position).getRfid());
                    startActivity(intent1);
                    finish();
                }
            }
        });
        mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findViewById(R.id.main_swipe);
        mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE, Color.WHITE);//设置水滴中旋转动画的颜色，参数1:水滴落下过程中的颜色，参数2:水滴已经落下后的颜色
        mWaveSwipeRefreshLayout.setWaveColor(0xFF3794fc);//设置水滴样色
        mWaveSwipeRefreshLayout.setMaxDropHeight(1500);//设置最大下拉高度
        mWaveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {//模拟3秒的加载过程
                    @Override
                    public void run() {
                        load();
                        //接下来可以刷新ListView
                    }
                }, 1200);
            }

        });
    }

    private void load() {
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("repairsId",repairsId)
                            .add("source", Since_case.set().getSource())
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222",responseData.toString());
                    showResponse(responseData);

                } catch (IOException e) {
                    e.printStackTrace();

                }


            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(() -> {
            if (responseData.equals("[]")){
                Toast.makeText(Repairs_property.this,"无数据",Toast.LENGTH_SHORT).show();
                finish();
            }
            data.clear();
            Bitmap bitmap =null;
            mWaveSwipeRefreshLayout.setRefreshing(false);//停止加载动画
            Gson gson = new Gson();
            List<Property_bean> userList = gson.fromJson(responseData, new TypeToken<List<Property_bean>>(){}.getType());
            for(int i = 0;i < userList.size();i++){
                String assetName = userList.get(i).getAssetName();
                String deptId = userList.get(i).getDeptId();
                String assetNumber =userList.get(i).getAssetNumber();
                String userId = userList.get(i).getUserId();
                String model = userList.get(i).getModel();
                String type = userList.get(i).getType();
                String remark = userList.get(i).getDetail();
                String assetDetailId = userList.get(i).getAssetDetailId();
                if (userList.get(i).getImage().equals("")){
                    /*InputStream is = getResources().openRawResource(R.mipmap.null_po);
                    bitmap = BitmapFactory.decodeStream(is);*/
                }else {
                    byte [] input = Base64.decode(userList.get(i).getImage(), Base64.DEFAULT);
                    bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);
                }
                data.add(new AddRepairs_bean(bitmap,assetName,deptId,assetNumber,userId,model,type,remark,"",assetDetailId));

            }
            Addrepairs_Adapter adapter_item = new Addrepairs_Adapter(Repairs_property.this,R.layout.input_adapter_item,data);
            lv_poperty.setAdapter(adapter_item);
        });
    }

    @Override
    protected void onRestart() {
        load();
        super.onRestart();

    }
}
